package com.deer.remote.server.rds.controller;

import com.deer.remote.common.encrypt.MD5;
import com.deer.remote.common.protocol.Command;
import com.deer.remote.common.protocol.Message;
import com.deer.remote.common.protocol.Packet;
import com.deer.remote.common.util.Configs;
import com.deer.remote.common.util.Log;
import com.deer.remote.server.rds.BaseMessageController;
import com.deer.remote.server.rds.Client;
import com.deer.remote.server.rds.SessionManager;
import com.deer.remote.server.rds.TentacleDesktopSession;

/**
 * Created by matrixy on 2019/1/3. 受控端连接发起时的会话认证处理
 */
public class AuthenticateController extends BaseMessageController {
	@Override
	public boolean authenticateRequired() {
		return false;
	}

	@Override
	public Message service(TentacleDesktopSession session, Message msg) throws Exception {
		Packet body = msg.getBody();
		String name = new String(body.nextBytes(body.nextInt()), "UTF-8");

		String nonce = new String(body.nextBytes(32));
		String signature = new String(body.nextBytes(32));

		// TODO: 可在这里通过com.deer.remote.server.util.BeanUtils.createBean()来创建Spring
		// Bean进行数据库查询认证
		if (signature.equals(MD5.encode(nonce + ":::" + Configs.get("client.key"))) == false) {
			this.replyAndDisconnect();
			Log.error(String.format("unauth connection: %s", session.getRemoteAddress()));
			Message resp = new Message().withCommand(Command.AUTHENTICATE_RESPONSE)
					.withBody(Packet.create(1).addByte((byte) 0x01));
			return resp;
		}

		System.out.println("Authenticated: " + name);

		Client info = new Client();
		info.setName(name);
		info.setControlling(false);
		info.setAddress(session.getRemoteAddress());

		session.setClient(info);
		SessionManager.register(session);

		Message resp = new Message().withCommand(Command.AUTHENTICATE_RESPONSE)
				.withBody(Packet.create(1).addByte((byte) 0x00));
		return resp;
	}
}
